package com.foreveredu.propms.web.struts.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.ChargeItem;
import com.foreveredu.propms.domain.District;
import com.foreveredu.propms.domain.House;
import com.foreveredu.propms.domain.SubDistrict;
import com.foreveredu.propms.domain.User;
import com.foreveredu.propms.service.IChargeService;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.RegHouseActionForm;

/**
 * 处理执行房产登记操作请求
 * 
 * @author hushigang
 * 
 */
public class RegHouseAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;
	private IChargeService chargeService = null;

	public IChargeService getChargeService() {
		return chargeService;
	}

	public void setChargeService(IChargeService chargeService) {
		this.chargeService = chargeService;
	}

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 跳转到房产登记页面
	public ActionForward reg(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获得日志记录器
//		Logger log = Logger.getLogger(RegHouseAction.class);
		
		// 获取当前登录用户
		User user = null;
		try {
			user = managementService.findUserByUsername(this.getPrincipal()
				.getUsername());
		} catch(ClassCastException e) {
			//用户长时间不操作，session丢失后，跳转到登录页面
			return mapping.findForward("login");
		}
//		Set<Role> roles = user.getRoles();
//		log.info("roles.size="+roles.size());
		
		// 获取当前物业管理员所在的物业服务处
		District dis = user.getDistrict();
//		log.info("dis.name="+dis.getName());
		request.setAttribute("dis", dis);
		
		// 获取该物业管理处下的小区
		Set<SubDistrict> subs = dis.getSubs();
//		log.info("subs.size="+subs.size());
		List<SubDistrict> subList = new ArrayList<SubDistrict>();
		subList.addAll(subs);
		request.setAttribute("subList", subList);
		
		// 列出小区中的楼栋，以及楼栋中的房产
		
		// 列出所有的收费项目
		List<ChargeItem> itemList = chargeService.findChargeItems();
		request.setAttribute("itemList", itemList);

		return mapping.findForward("houseReg");
	}

	// 处理房产登记操作请求
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		//获得当前进行登记操作的房产对象
		RegHouseActionForm raf = (RegHouseActionForm)form;
		
		//获得表单的处理方式编码
		int handleId = raf.getHandleId();
		
		//获得表单的小区编码
		int subId = raf.getSubId();
		SubDistrict subDistrict = managementService.findSubDistrictById(subId);
	
		Set<ChargeItem> itemSet = new LinkedHashSet<ChargeItem>();
		
		//从表单中获取选择的收费项目
		String[] itemIds = raf.getItemIds();
		for(int i = 0;i<itemIds.length;i++){
			ChargeItem ci = chargeService.findChargeItemById(Integer.parseInt(itemIds[i]));
			itemSet.add(ci);
		}
		
		if(handleId == 1){
			//批量操作，通过小区获得该小区的所有房产
			List<House> houseList = managementService.findAllHousesBySubDistrict(subDistrict);
			
			/*
			 * 完成房产对象的登记操作，
			 * 1、指定该房产的收费项目
			 * 2、对房产进行初始化操作，即指定开始收费日期
			 */
			
			//获取房产登记的初始时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String initDate = sdf.format(new Date());
			
			//设置该小区的所有房产的收费项目
			for(House house : houseList){
				house.setChargeItems(itemSet);
				//设置该房产是否已经登记了
				house.setIsReg(1);
				//设置该房产是否已经初始化收费了
				house.setIsInit(1);
				//设置该房产收费起始时间
				house.setInitDate(initDate);
				managementService.updateHouse(house);
			}
			
		} else if(handleId == 2){
			//个别操作
			int houseId = raf.getHouseId();
			House house = managementService.findHouseById(houseId);
			request.setAttribute("house", house);
			
			house.setChargeItems(itemSet);
			
			house.setIsReg(1);
			house.setIsInit(1);
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String initDate = sdf.format(new Date());
			house.setInitDate(initDate);
			
			managementService.updateHouse(house);
		}
		
		request.setAttribute("itemSet", itemSet);
		
		return mapping.findForward("save");
	}

}
